﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.BLL.GroupMember;
using System.Data;
using MINERP.COM;
using System.Windows.Forms;
using MINERP.MODEL.GroupMember;
using MINERP.MODEL.FoundationModel;

namespace MINERP.GroupMember.Presenter
{
    public class FrmTaskListPresenter
    {
        private IView.IFrmTasklist _view;
        private GroupMemberBLL _bll;
        WorkShop_Info workshop; 

        private DataTable _dtSource;//用于存放任务列表的数据

        public FrmTaskListPresenter(IView.IFrmTasklist view)
        {
            _view = view;
            _bll = new GroupMemberBLL();
            ShowTask(0);
            workshop = _bll.GetWorkShopInfo();
        }


        public void SelectedTask(TaskInfo task)
        {
            TaskInfo selectTI = _view.selectTaskInfo;
            if (selectTI!=null)
            {
                if (workshop.ControlNonexistenceTechByPriority)//按加急控制
                {
                    //查看选择的产品是否是列表中加急级别最高的
                    int MaxPriority = DataTransfer.taskList.Where(t => t.status != 0).Max(t => t.Priority);
                    if (MaxPriority != selectTI.Priority && task.TechName != "看样")
                    {
                        //if (DialogResult.OK==MessageBox.Show("你选择的不是最高加级的任务，是否继续？\n如果选择否则自动取得最高级的任务","提示",MessageBoxButtons.OKCancel ,MessageBoxIcon.Question))
                        //{
                        //    DataTransfer.SelectTaskInfo = selectTI;
                        //    DataTransfer.taskFrmIsOpen = false;
                        //    _view.Close();
                        //}
                        //else//如果选择否则自动取得最高级别的任务
                        //{
                        //    DataTransfer.SelectTaskInfo = DataTransfer.taskList.OrderByDescending(t=>t.Priority).FirstOrDefault();
                        //    DataTransfer.taskFrmIsOpen = false;
                        //    _view.Close();
                        //}
                        MessageBox.Show("你选择的不是最高加级的任务");

                    }
                    else
                    {
                        DataTransfer.SelectTaskInfo = selectTI;
                        DataTransfer.taskFrmIsOpen = false;
                        _view.Close();
                    }
                }
                else//不按加急级别控制
                {
                    DataTransfer.SelectTaskInfo = selectTI;
                    DataTransfer.taskFrmIsOpen = false;
                    _view.Close();
                }
            }
        }

        public void ShowTask(int LookType)
        {
            int picCount = 0;
            double picAreaCount = 0.0;

            if (LookType == 1)//表示强制刷新
            {
                DataTransfer.taskList = _bll.GetListTask(DataTransfer.UserId, out picCount, out picAreaCount);
                DataTransfer.PicCount = picCount;
                DataTransfer.PicAreaCount = picAreaCount;
            }
            else
            {
                if (DataTransfer.taskList.Count < 1)
                {
                    DataTransfer.taskList = _bll.GetListTask(DataTransfer.UserId, out picCount, out picAreaCount);

                    DataTransfer.PicCount = picCount;
                    DataTransfer.PicAreaCount = picAreaCount;
                }
            }
           // DataTransfer.taskList=DataTransfer.taskList.OrderByDescending(t => t.Priority).ToList();
            _view.ShowTasks(DataTransfer.taskList, DataTransfer.PicCount, DataTransfer.PicAreaCount);          
        }

        public void ShowTask()
        {
            if (_view.LookType < 0)
            {
                ShowTask(0);
            }
            else
            {
                ShowTask(_view.LookType);
            }
        }
        /// <summary>
        /// 客户收取所有的未收取任务
        /// </summary>
        internal void ReceiveAllTasks()
        {
            if (_bll.ReceiveAllTask(DataTransfer.UserId))
            {
                ShowTask();
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("获取任务失败！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
            }
        }
    }
}
